﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using RMM.UserAdminService;
using System.Threading.Tasks;

namespace RMM.Web.Controllers
{
    public class LoginController : Controller
    {
        //LoginAdminAsync
        private readonly IUserAdminService _userAdminService;

        public LoginController(IUserAdminService userAdminService)
        {
            _userAdminService = userAdminService;
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public async Task<JsonResult> LoginAdmin(string id)
        {
            var username = Request["postData[id]"];
            var pwd = Request["postData[pwd]"];
            var user = await _userAdminService.LoginAdminAsync(username, pwd);
            //HttpContext.Response.Cookies.Add(new HttpCookie("currId", id));
            //HttpContext.Response.Cookies.Add(new HttpCookie("currName", "admin"));
            Msg msg = new Msg();
            if (user == null)
            {
                msg.msg = "请验证用户名和密码";
                msg.code = 1;
            }
            else
            {
                msg.code = 0;
                msg.msg = "登陆成功";
                Session["userid"] = user.UserId;
                Session["username"] = user.UserName;
                Session["userhead"] = user.UserHead;
                Session["ship"] = user.ShopId;
                HttpContext.Response.Cookies.Add(new HttpCookie("userid", user.UserId.ToString()));//存储用户id在本地以免session丢失
            }
            return Json(msg);
        }
    }

    public class Msg
    {
        public int code { get; set; }
        public string msg { get; set; }
    }
}